package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */


@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoService;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Override
    public void saveOrUpdatebyvo(RoomSubmitVo roomSubmitVo) {
        // 检查房间对象是否有ID，如果有，则执行更新操作，否则执行新增操作
        boolean isupdate = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);
        // 如果是更新操作，则需要先删除旧的数据
        if (isupdate) {
            // 删除与房间关联的图片信息
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM); // 指定图片类型为房间
            queryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId()); // 指定房间ID
            graphInfoService.remove(queryWrapper); // 删除符合条件的图片信息

            // 删除房间标签信息
            LambdaQueryWrapper<RoomLabel> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(RoomLabel::getRoomId, roomSubmitVo.getId()); // 指定房间ID
            roomLabelService.remove(queryWrapper1); // 删除房间的所有标签信息

            // 删除房间属性
            LambdaQueryWrapper<RoomAttrValue> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId()); // 指定房间ID
            roomAttrValueService.remove(queryWrapper2); // 删除房间的所有属性信息

            // 删除房间配套
            LambdaQueryWrapper<RoomFacility> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.eq(RoomFacility::getRoomId, roomSubmitVo.getId()); // 指定房间ID
            roomFacilityService.remove(queryWrapper3); // 删除房间的所有配套信息

            // 删除支付方式
            LambdaQueryWrapper<RoomPaymentType> queryWrapper4 = new LambdaQueryWrapper<>();
            queryWrapper4.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId()); // 指定房间ID
            roomPaymentTypeService.remove(queryWrapper4); // 删除房间的所有支付方式信息

            // 删除租期方式
            LambdaQueryWrapper<RoomLeaseTerm> queryWrapper5 = new LambdaQueryWrapper<>();
            queryWrapper5.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId()); // 指定房间ID
            roomLeaseTermService.remove(queryWrapper5); // 删除房间的所有租期方式信息
            //删除缓存
            String key= RedisConstant.APP_LOGIN_PREFIX+roomSubmitVo.getId();
            redisTemplate.delete(key);
        }

        // 添加图片信息到数据库
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) { // 检查图片列表是否为空
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) { // 遍历图片列表
                GraphInfo graphInfo = new GraphInfo(); // 创建图片信息对象
                graphInfo.setItemType(ItemType.ROOM); // 设置图片类型为房间
                graphInfo.setItemId(roomSubmitVo.getId()); // 设置图片关联的房间ID
                graphInfo.setUrl(graphVo.getUrl()); // 设置图片的URL
                graphInfo.setName(graphVo.getName()); // 设置图片的名称
                graphInfos.add(graphInfo); // 将图片信息添加到列表中
            }
            graphInfoService.saveBatch(graphInfos); // 批量保存图片信息到数据库
        }

        // 添加房间属性值
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) { // 检查属性值列表是否为空
            List<RoomAttrValue> attrValueVos = new ArrayList<>();
            for (Long attrValueId : attrValueIds) { // 遍历属性值列表
                RoomAttrValue roomAttrValue = new RoomAttrValue(); // 创建房间属性值对象
                roomAttrValue.setRoomId(roomSubmitVo.getId()); // 设置属性值关联的房间ID
                roomAttrValue.setAttrValueId(attrValueId); // 设置属性值的ID
                attrValueVos.add(roomAttrValue); // 将房间属性值添加到列表中
            }
            roomAttrValueService.saveBatch(attrValueVos); // 批量保存房间属性值到数据库
        }

        // 添加房间配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) { // 检查配套信息列表是否为空
            List<RoomFacility> facilityVos = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) { // 遍历配套信息列表
                RoomFacility roomFacility = new RoomFacility(); // 创建房间配套对象
                roomFacility.setRoomId(roomSubmitVo.getId()); // 设置配套信息关联的房间ID
                roomFacility.setFacilityId(facilityInfoId); // 设置配套信息的ID
                facilityVos.add(roomFacility); // 将房间配套添加到列表中
            }
            roomFacilityService.saveBatch(facilityVos); // 批量保存房间配套信息到数据库
        }

        // 添加房间标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) { // 检查标签信息列表是否为空
            List<RoomLabel> labelVos = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) { // 遍历标签信息列表
                RoomLabel label = new RoomLabel(); // 创建房间标签对象
                label.setRoomId(roomSubmitVo.getId()); // 设置标签信息关联的房间ID
                label.setLabelId(labelInfoId); // 设置标签信息的ID
                labelVos.add(label); // 将房间标签添加到列表中
            }
            roomLabelService.saveBatch(labelVos); // 批量保存房间标签信息到数据库
        }

        // 添加支付方式信息
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) { // 检查支付方式列表是否为空
            List<RoomPaymentType> paymentTypeVos = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) { // 遍历支付方式列表
                RoomPaymentType roomPaymentType = new RoomPaymentType(); // 创建房间支付方式对象
                roomPaymentType.setRoomId(roomSubmitVo.getId()); // 设置支付方式关联的房间ID
                roomPaymentType.setPaymentTypeId(paymentTypeId); // 设置支付方式的ID
                paymentTypeVos.add(roomPaymentType); // 将房间支付方式添加到列表中
            }
            roomPaymentTypeService.saveBatch(paymentTypeVos); // 批量保存房间支付方式信息到数据库
        }

        // 添加租期方式信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) { // 检查租期方式列表是否为空
            List<RoomLeaseTerm> leaseTermVos = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) { // 遍历租期方式列表
                RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm(); // 创建房间租期方式对象
                roomLeaseTerm.setRoomId(roomSubmitVo.getId()); // 设置租期方式关联的房间ID
                roomLeaseTerm.setLeaseTermId(leaseTermId); // 设置租期方式的ID
                leaseTermVos.add(roomLeaseTerm); // 将房间租期方式添加到列表中
            }
            roomLeaseTermService.saveBatch(leaseTermVos); // 批量保存房间租期方式信息到数据库
        }
    }

    @Override
    public IPage<RoomItemVo> pageitem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {


        return roomInfoMapper.pageItem(page,queryVo);
    }

    @Override
    public RoomDetailVo ByIdstolist(Long id) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        ApartmentInfo apartmentInfo = apartmentInfoService.selectById(roomInfo.getApartmentId());
        List<GraphVo> graphVoList=graphInfoMapper.getvo(ItemType.ROOM,id);
        List<AttrValueVo> attrValueVoList=attrValueMapper.listbyroomid(id);
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectListbyroomid(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
        List<PaymentType> paymentTypeList=paymentTypeMapper.selectListbyroomid(id);
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListbyroomid(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void removeroomById(Long id) {
        super.removeById(id);
        LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM); // 指定图片类型为房间
        queryWrapper.eq(GraphInfo::getItemId,id); // 指定房间ID
        graphInfoService.remove(queryWrapper); // 删除符合条件的图片信息

        // 删除房间标签信息
        LambdaQueryWrapper<RoomLabel> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(RoomLabel::getRoomId, id); // 指定房间ID
        roomLabelService.remove(queryWrapper1); // 删除房间的所有标签信息

        // 删除房间属性
        LambdaQueryWrapper<RoomAttrValue> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(RoomAttrValue::getRoomId,id); // 指定房间ID
        roomAttrValueService.remove(queryWrapper2); // 删除房间的所有属性信息

        // 删除房间配套
        LambdaQueryWrapper<RoomFacility> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(RoomFacility::getRoomId, id); // 指定房间ID
        roomFacilityService.remove(queryWrapper3); // 删除房间的所有配套信息

        // 删除支付方式
        LambdaQueryWrapper<RoomPaymentType> queryWrapper4 = new LambdaQueryWrapper<>();
        queryWrapper4.eq(RoomPaymentType::getRoomId, id); // 指定房间ID
        roomPaymentTypeService.remove(queryWrapper4); // 删除房间的所有支付方式信息

        // 删除租期方式
        LambdaQueryWrapper<RoomLeaseTerm> queryWrapper5 = new LambdaQueryWrapper<>();
        queryWrapper5.eq(RoomLeaseTerm::getRoomId, id); // 指定房间ID
        roomLeaseTermService.remove(queryWrapper5); // 删除房间的所有租期方式信息

        //删除缓存
       String key= RedisConstant.APP_LOGIN_PREFIX+id;
        redisTemplate.delete(key);
    }


}




